﻿using Model.Extend;
using Model.sys;
using System.Text;

namespace CToolsWin.DAL.sys
{
    public class DALSqlConn : IDAL.BaseModelADO<sys_SqlConn>
    {
        /// <summary>
        /// 初始化表
        /// </summary>
        public bool InitTable()
        {
            return DBInit.TruncateTable(ModelAttribute.GetTableName<sys_SqlConn>());
        }
        public bool IsExists(sys_SqlConn m)
        {
            StringBuilder sql = new StringBuilder();
            if (m.ID > 0)
            {
                sql.Append(" AND [ID]<>@ID");
            }
            if (string.IsNullOrWhiteSpace(m.Name) == false)
            {
                sql.AppendFormat(" AND Name='{0}'", m.Name);
            }
            if (string.IsNullOrWhiteSpace(m.Code) == false)
            {
                sql.AppendFormat(" AND Code='{0}'", m.Code);
            }
            return db.Exists(ModelAttribute.GetTableName<sys_SqlConn>(), sql.ToString(), DB.ADOHelper.SetParmValues(m, m.ID > 0));
        }
        public sys_SqlConn GetModel(sys_SqlConn m)
        {
            StringBuilder sql = new StringBuilder();
            if (m.ID > 0)
            {
                sql.Append(" AND [ID]<>@ID");
            }
            if (string.IsNullOrWhiteSpace(m.Name) == false)
            {
                sql.AppendFormat(" AND Name='{0}'", m.Name);
            }
            if (string.IsNullOrWhiteSpace(m.Code) == false)
            {
                sql.AppendFormat(" AND Code='{0}'", m.Code);
            }

            if (string.IsNullOrEmpty(sql.ToString()))
                sql.Append(" AND 1=2 ");
            return db.GetModelText<sys_SqlConn>(sql.ToString(), DB.ADOHelper.SetParmValues(m, m.ID > 0));
        }
    }
}
